package com.julis.ui.Dialog;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.julis.R;

import java.util.ArrayList;
import java.util.HashMap;

public class MultiSelectDialog implements View.OnClickListener {
 
    Context context;
    MultiSelectInterface selectInterface;
    AlertDialog dialog;
    RelativeLayout tvCancel, tvSure;
    ListView lvContent;
    ArrayList<HashMap<String, Object>> listData;
 
    public MultiSelectDialog(Context context, ArrayList<HashMap<String, Object>> listData, MultiSelectInterface selectInterface) {
        this.context = context;
        this.selectInterface = selectInterface;
        this.listData = listData;
 
        dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_multiselect);
        //初始化控件
        tvCancel = (RelativeLayout) window.findViewById(R.id.tvCancel);
        tvSure = (RelativeLayout) window.findViewById(R.id.tvSure);
        lvContent = (ListView) window.findViewById(R.id.lvContent);
        tvCancel.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        //设置适配器
        MultiSelectAdapter adapter = new MultiSelectAdapter();
        lvContent.setAdapter(adapter);
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvCancel:
                dialog.dismiss();
                break;
            case R.id.tvSure:
                dialog.dismiss();
                selectInterface.sure(listData, retSelect());
                break;
        }
    }
 
    //整理被选中的item
    private int[] retSelect() {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < listData.size(); i++) {
            if ((boolean) listData.get(i).get("isSelect")) {
                list.add(i);
            }
        }
        int[] select = new int[list.size()];
        for (int i = 0; i < select.length; i++) {
            select[i] = list.get(i);
        }
        return select;
    }
 
    //List适配器
    private class MultiSelectAdapter extends BaseAdapter {
 
        public MultiSelectAdapter() {
        }
 
        @Override
        public int getCount() {
            return listData.size();
        }
 
        @Override
        public Object getItem(int position) {
            return null;
        }
 
        @Override
        public long getItemId(int position) {
            return position;
        }
 
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_multiselect, null);
                holder = new ViewHolder();
                holder.chkSelect = (CheckBox) convertView.findViewById(R.id.chkSelect);
                holder.tvContent = (TextView) convertView.findViewById(R.id.tvContent);
                holder.llItem = (RelativeLayout) convertView.findViewById(R.id.llItem);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            //初始设置
            if ((boolean) listData.get(position).get("isSelect")) {
                holder.chkSelect.setChecked(true);
            } else {
                holder.chkSelect.setChecked(false);
            }
            holder.tvContent.setText(listData.get(position).get("text").toString());
            //CheckBox选择监听
            holder.chkSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editSync(position);
                }
            });
            holder.llItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editSync(position);
                }
            });
            return convertView;
        }
 
        // 修改指定key数据、刷新列表
        private void editSync(int position){
            for (HashMap<String, Object> hashMap : listData) {
                if ((int) hashMap.get("position") == position) {
                    hashMap.put("isSelect", !(boolean) listData.get(position).get("isSelect"));
                    MultiSelectAdapter.this.notifyDataSetChanged();
                }
            }
        }
 
        private class ViewHolder {
            CheckBox chkSelect;
            TextView tvContent;
            RelativeLayout llItem;
        }
    }
}
